/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.service.payment.provider.cybersource.context;

import java.util.Enumeration;
import java.util.Properties;

/**
 * Provides a CyberSource payment services configuration.
 * 
 * @author Scott Rossillo
 *
 */
public class CyberSourceConfiguration {
	
	/**
	 * The default CyberSource server id, CyberSource_SJC_US.
	 */
	public final static String DEFAULT_SERVER_ID = "CyberSource_SJC_US";
	
	/**
	 * The default target API version, 1.32. 
	 */
	public final static String DEFAULT_TARGET_API_VERSION = "1.32";
	
	private String merchantId;
	private String serverId;
	private String targetAPIVersion;
	private String keysDirectory;
	
	private boolean useOrderTotal;
	private boolean sendToProduction;
	
	private Properties additionalProperties = null;
	
	/**
	 * Creates a new CyberSource configuration.
	 */
	public CyberSourceConfiguration() {
		useOrderTotal = false;
		sendToProduction = false;
		serverId = DEFAULT_SERVER_ID;
		targetAPIVersion = DEFAULT_TARGET_API_VERSION;
	}

	public String getMerchantId() {
		return merchantId;
	}

	public void setMerchantId(String merchantId) {
		this.merchantId = merchantId;
	}
	
	public boolean isUseOrderTotal() {
		return useOrderTotal;
	}

	public void setUseOrderTotal(boolean useOrderTotal) {
		this.useOrderTotal = useOrderTotal;
	}
	
	public String getServerId() {
		return serverId;
	}

	public void setServerId(String serverId) {
		this.serverId = serverId;
	}

	public String getTargetAPIVersion() {
		return targetAPIVersion;
	}

	public void setTargetAPIVersion(String targetAPIVersion) {
		this.targetAPIVersion = targetAPIVersion;
	}

	public String getKeysDirectory() {
		return keysDirectory;
	}

	public void setKeysDirectory(String keysDirectory) {
		this.keysDirectory = keysDirectory;
	}

	public boolean isSendToProduction() {
		return sendToProduction;
	}

	public void setSendToProduction(boolean sendToProduction) {
		this.sendToProduction = sendToProduction;
	}
	
	public void setAdditionalProperties(Properties additionalProperties) {
		this.additionalProperties = additionalProperties;
	}
	
	private void addAdditionalProperties(Properties props) {
		String propName;
		if(additionalProperties != null) {
			 for (Enumeration e = additionalProperties.propertyNames(); e.hasMoreElements(); ) {
				 propName = (String) e.nextElement();
		         props.setProperty(propName, additionalProperties.getProperty(propName));
		     }
		}
	}

	/**
	 * Returns the field values of this configuration as properties.
	 * 
	 * @return the key/value <code>Properties</code> derived from this
	 * configuration
	 */
	public Properties toProperties() {
		
		Properties props = new Properties();
		
		addAdditionalProperties(props);
		
		props.setProperty("keysDirectory", keysDirectory);
		
		props.setProperty("merchantID", merchantId);
		props.setProperty("targetAPIVersion", targetAPIVersion);
		props.setProperty("sendToProduction", String.valueOf(sendToProduction));
		props.setProperty("serverID", serverId);
		
		return props;
	}

}
